<?php

namespace app\home\controller;

use think\Controller;

use think\facade\Request;

use think\facade\Session;

class Goods extends Base
{
    //分类下的商品列表
    public function index($id)
    {
        //$id 是分类id
        $id=input()['id'];
        // $goods_id=input()['id'];
        $list=\app\common\model\Goods::where('cate_id',$id)->order('id desc')->paginate(10);
        $cate_info=\app\common\model\Category::find($id);

        return view('index',['list'=>$list,'cate_info'=>$cate_info]);
    }
    public function detail($id){ 
        
        // $id=input()['id'];
        
        // $goods_images = \app\common\model\GoodsImages::find($goods_id);


        $goods = \app\common\model\Goods::with('goods_images')->find($id);
        $comment=\app\common\model\Comment::where('goods_id',$id)->select();
        // dump($goods);die;
        $goods['goods_images']=\app\common\model\GoodsImages::where('goods_id',$id)->select();
        // dump($goods_images);die;
         $goods['goods_attr']=json_decode($goods['goods_attr'],true);
        // dump($goods);die;
        // dump($goods['goods_images']);die;
       //转化商品属性的json为数组
        return view('detail',['goods' => $goods,'goods_images'=>$goods['goods_images'],'comment'=>$comment,'goods_id'=>$id]);
    }

    
    public function comment(){
        $param=Request::param();
        $user_id=session('user_info.id');
        
        $data=[
            'user_id'=>$user_id,
            'nickname'=>Session::get('nickname'),
            'goods_id'=>$param['goods_id'],
            'goods_comment'=>$param['goods_comment'],
           
            

        ];
        // dump($data);
        \app\common\model\Comment::create($data,true);
        $this->redirect('home/goods/detail',['id'=>$param['goods_id']]);

        

    }


}
